﻿using Common;
using Printers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using ZXing;

namespace WinFormsPrintDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //初始化打印机下拉列表选项
            InitprinterComboBox();
            //this.BackgroundImage = QrCodeUtil.GenByZXingNet("http://:4050/admin.html?ip=");

            this.pictureBox1.BackgroundImageLayout = ImageLayout.Center;
            //this.pictureBox1.BackgroundImage = QrCodeUtil.Code128("19061799999001", 300,40);
        }


        /// <summary>
        /// 测试数据
        /// </summary>
        /// <returns></returns>
        private DataTable TestDate()
        {
            DataTable dt = new DataTable();
            //实例化三个列
            DataColumn dc1 = new DataColumn("第一列", System.Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("factoryPosition", System.Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("factoryHistory", Type.GetType("System.String"));
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);

            //添加三条数据　　　　

            DataRow row = dt.NewRow();
            row["第一列"] = "微软";
            row["factoryPosition"] = "北京";
            row["factoryHistory"] = "100years";
            dt.Rows.Add(row);

            row = dt.NewRow();

            row["第一列"] = "微软";
            row["factoryPosition"] = "北京";
            row["factoryHistory"] = "100years";
            dt.Rows.Add(row);

            row = dt.NewRow();

            row["第一列"] = "微软";
            row["factoryPosition"] = "北京";
            row["factoryHistory"] = "100years";
            dt.Rows.Add(row);
            return dt;
        }

        /// <summary>
        /// 打印列表
        /// </summary>
        private void InitprinterComboBox()
        {
            List<String> list = LocalPrinter.GetLocalPrinters(); //获得系统中的打印机列表
            foreach (String s in list)
            {
                printerComboBox.Items.Add(s); //将打印机名称添加到下拉框中
            }
        }



        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, EventArgs e)
        {
            new PrintHelper().Print(this.TestDate(), "asdf");
        }

        /// <summary>
        /// 打印预览
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender, EventArgs e)
        {
            new PrintHelper().PrintPriview(this.TestDate(), "asdf");
        }

        /// <summary>
        /// 选择打印机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button3_Click(object sender, EventArgs e)
        {
            if (printerComboBox.SelectedItem != null) //判断是否有选中值
            {
                if (Externs.SetDefaultPrinter(printerComboBox.SelectedItem.ToString())) //设置默认打印机
                {
                    float width = 20;
                    float height = 20;
                    PrintDocument printDocument1 = new PrintDocument();
                    MyPageSize ps = new MyPageSize();
                    string defaultPrinterName = printerComboBox.SelectedItem.ToString(); //默认打印机名称  
                    string myPageSizeName = "MyCustomePageSize";
                    ps.SetPrintForm(defaultPrinterName, myPageSizeName, width, height);
                    PaperSize ps1 = ps.GetPrintForm(defaultPrinterName, myPageSizeName);
                    printDocument1.DefaultPageSettings.PaperSize = ps1;
                    MessageBox.Show(printerComboBox.SelectedItem.ToString() + "设置为默认打印机成功！");
    
                }
                else
                {
                    MessageBox.Show(printerComboBox.SelectedItem.ToString() + "设置为默认打印机失败！");
                }
            }
        }


        /// <summary>
        /// 打印表格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button4_Click(object sender, EventArgs e)
        {
            List<PsdEntity> psdData = HttpHelper.GetHttpRequest<List<PsdEntity>>("http://tg.cakeerp.cn:81/json.js");
            new TablePrint(psdData).PrintPriview();//这是浏览函数，需要打印请调用print();
            //new TablePrint(psdData).Print();//这是浏览函数，需要打印请调用print();
        }


   
    }
}
